﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Code4WP7SDK;
using System.Collections.ObjectModel;
using LuWeibo.Models;
using System.Collections.Generic;
using LuWeiboSDK;
using Newtonsoft.Json;
using System.ComponentModel;
using System.Windows.Resources;
using System.IO;

namespace LuWeibo.ViewModels
{
    public class StatusViewModel : BaseModel
    {
        #region Fields
        private ICommand _showWeiboCommand;
        private ObservableCollection<Status> _showStatus;
        private ICommand _publishStatusCommand;
        private Status _selectedStatus;
        private ICommand _goToStatusDetailCommand;
        private string _pubText; //要发布的微博信息
        #endregion

        #region Properties
        public ObservableCollection<Status> ShowStatus
        {
            get
            {
                if (this._showStatus == null)
                {
                    this._showStatus = new ObservableCollection<Status>();
                    #region InDesign
                    if (DesignerProperties.IsInDesignTool)
                    {
                        Status status = new Status
                        {
                            created_at = "Thu Mar 29 10:13:06 +0800 2012",
                            id = 3428859137993491,
                            mid = "3428859137993491",
                            idstr = "3428859137993491",
                            text = "好爸爸十条标准，你是好爸爸吗？[可爱]",
                            source = "<a href=\"http://weibo.com\" rel=\"nofollow\">新浪微博</a>",
                            favorited = false,
                            truncated = false,
                            in_reply_to_status_id = "",
                            in_reply_to_user_id = "",
                            in_reply_to_screen_name = "",
                            geo = null,
                            IsShareVisible = true,
                            user = new User
                            {
                                id = "1713926427",
                                idstr = "1713926427",
                                screen_name = "微博搞笑排行榜",
                                name = "微博搞笑排行榜",
                                province = "44",
                                city = "1",
                                location = "广东 广州",
                                description = "微博搞笑中心！每天搜罗最搞笑最好玩的微博。关注我，获得每日新鲜笑料！（欢迎合作，投稿）↖(^ω^)↗",
                                url = "http://weibo.com/mkdqs",
                                profile_image_url = "http://tp4.sinaimg.cn/1713926427/50/5615669283/0",
                                profile_url = "topgirls8",
                                domain = "topgirls8",
                                weihao = "",
                                gender = "f",
                                followers_count = 7199354,
                                friends_count = 915,
                                statuses_count = 36517,
                                favourites_count = 4437,
                                created_at = "Fri Mar 19 00:00:00 +0800 2010",
                                following = true,
                                allow_all_act_msg = true,
                                geo_enabled = false,
                                verified = false,
                                remark = "",
                                allow_all_comment = true,
                                avatar_large = "http://tp4.sinaimg.cn/1713926427/180/5615669283/0",
                                verified_reason = "",
                                follow_me = false,
                                online_status = 1,
                                bi_followers_count = 278,
                            },
                            retweeted_status = new Status
                            {
                                created_at = "Wed Mar 28 22:12:14 +0800 2012",
                                id = 3428677721732850,
                                mid = "3428677721732850",
                                idstr = "3428677721732850",
                                text = "【好爸爸十条标准】1.下班就回家。2.到家先陪孩子玩。3.不训斥，不打孩子。4.不当着孩子面吵架。5.不当着孩子面抽烟。6.不当着孩子面上网玩游戏。7.当孩子面对帮你的家人说“谢谢”。8.当孩子面洗自己的臭袜子。9.当着孩子面把碗里的饭菜吃干净。10.当全家人的面夸奖做对事的孩子。爸爸们加油！！",
                                source = "<a href=\"http://weibo.com\" rel=\"nofollow\">新浪微博</a>",
                                favorited = false,
                                truncated = false,
                                in_reply_to_status_id = "",
                                in_reply_to_user_id = "",
                                in_reply_to_screen_name = "",
                                thumbnail_pic = "http://ww2.sinaimg.cn/thumbnail/6a1f0209gw1drfvb2y0kvj.jpg",
                                bmiddle_pic = "http://ww2.sinaimg.cn/bmiddle/6a1f0209gw1drfvb2y0kvj.jpg",
                                original_pic = "http://ww2.sinaimg.cn/large/6a1f0209gw1drfvb2y0kvj.jpg",
                                geo = null,
                                user = new User
                                {
                                    id = "1780417033",
                                    idstr = "1780417033",
                                    screen_name = "实用小百科",
                                    name = "实用小百科",
                                    province = "44",
                                    city = "1",
                                    location = "广东 广州",
                                    description = "实用小百科，专门搜集生活中的实用小常识、小智慧。为您的生活提供更多帮助与便利。",
                                    url = "",
                                    profile_image_url = "http=//tp2.sinaimg.cn/1780417033/50/1280367872/0",
                                    profile_url = "xfxzh",
                                    domain = "xfxzh",
                                    weihao = "",
                                    gender = "f",
                                    followers_count = 2910068,
                                    friends_count = 476,
                                    statuses_count = 13670,
                                    favourites_count = 500,
                                    created_at = "Wed Jul 21 00=00=00 +0800 2010",
                                    following = true,
                                    allow_all_act_msg = true,
                                    geo_enabled = true,
                                    verified = false,
                                    remark = "",
                                    allow_all_comment = true,
                                    avatar_large = "http=//tp2.sinaimg.cn/1780417033/180/1280367872/0",
                                    verified_reason = "",
                                    follow_me = false,
                                    online_status = 0,
                                    bi_followers_count = 119,
                                }
                            }
                        };
                        this._showStatus.Add(status);
                        this._showStatus.Add(status);
                        this._showStatus.Add(status);
                        this._showStatus.Add(status);
                        this._showStatus.Add(status);
                        this._showStatus.Add(status);
                        //StreamResourceInfo sri = Application.GetResourceStream(new Uri("/Docs/Status.json",UriKind.Relative));
                        //using (StreamReader sr = new StreamReader(sri.Stream))
                        //{
                        //    string json = sr.ReadToEnd();
                        //    StatusModel weiboModel = JsonConvert.DeserializeObject<StatusModel>(json);
                        //    this.ShowStatus = weiboModel.statuses;
                        //}
                    }
                    #endregion
                }
                return _showStatus;
            }
            set
            {
                _showStatus = value;
                this.NotifyPropertyChanged(p => p.ShowStatus);
            }
        }

        public Status SelectedStatus
        {
            get { return _selectedStatus; }
            set
            {
                _selectedStatus = value;
                this.NotifyPropertyChanged(p => p.SelectedStatus);
            }
        }

        public string PubText
        {
            get { return _pubText; }
            set
            {
                _pubText = value;
                this.NotifyPropertyChanged(p => p.PubText);
            }
        }
        #endregion

        #region Command
        public ICommand ShowWeiboCommand
        {
            get
            {
                if (this._showWeiboCommand == null)
                {
                    this._showWeiboCommand = new BaseCommand(ShowWeiboExecute);
                }
                return _showWeiboCommand;
            }
        }

        public ICommand PublishStatusCommand
        {
            get
            {
                if (this._publishStatusCommand == null)
                {
                    this._publishStatusCommand = new BaseCommand<string>(PublishStatusExecute);
                }
                return _publishStatusCommand;
            }
        }

        public ICommand GoToStatusDetailCommand
        {
            get
            {
                if (this._goToStatusDetailCommand == null)
                {
                    this._goToStatusDetailCommand = new BaseCommand<Status>(GoToStatusDetailExecute);
                }
                return _goToStatusDetailCommand;
            }
        }
        #endregion

        #region Method
        public void ShowWeiboExecute()
        {
            //组合参数
            List<Parameter> paras = new List<Parameter>();
            string data = ParameterHelper.BuildParameters(paras);
            //发送请求
            WebClientService ws = new WebClientService();
            ws.WebClientCompleted += (s, e) =>
            {
                //Debug.WriteLine("str:" + e.Result.ToString());
                if (e.Ex == null)
                {
                    StatusModel weiboModel = JsonConvert.DeserializeObject<StatusModel>(e.Result.ToString());
                    this.ShowStatus = weiboModel.statuses;
                }
                else
                {

                }
            };
            ws.GetRequest(Urls.weibo_url, data);
            //接受数据
        }
        public void PublishStatusExecute(string statusText)
        {
            if (string.IsNullOrEmpty(statusText))
            {
                MessageBox.Show("微博信息不能为空，请输入发布的微博信息");
            }
            else
            {
                List<Parameter> paras = new List<Parameter>();
                paras.Add(new Parameter("status", HttpUtility.UrlEncode(statusText)));
                string data = ParameterHelper.BuildParameters(paras);
                WebClientService ws = new WebClientService();
                ws.WebClientCompleted += (s, e) =>
                {
                    if (e.Ex == null)
                    {
                        //发布微博成功
                        MessageBox.Show("微博发布成功!");
                        VmNavigateHelper.GoBack("/Views/Status/PostStatusPage.xaml");
                    }
                    else
                    {
                        //发布微博失败
                    }
                };
                ws.PostRequest(Urls.statuses_update, data);
            }
        }

        public void GoToStatusDetailExecute(Status selectedStatus)
        {
            ViewModelLocator.StatusDetailVMStatic.CurrentStatus = selectedStatus;
            VmNavigateHelper.Navigate("/Views/LuHomePage.xaml", "/Views/Status/StatusDeatailPage.xaml");
            this.SelectedStatus = null;//重置微博列表的选中项目
        }
        #endregion
    }
}
